package com.dongdongshop.controller;

import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.page.PageBean;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.service.AreasService;
import com.dongdongshop.service.CitiesService;
import com.dongdongshop.service.ProvincesService;
import com.dongdongshop.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    @DubboReference
    private AddressService addressService;

    @DubboReference
    private AreasService areasService;

    @DubboReference
    private CitiesService citiesService;

    @DubboReference
    private ProvincesService provincesService;

    @RequestMapping("send")
    public String send(){
        return "home-order-send";
    }

    @RequestMapping("show")
    public String show(){
        return "home-setting-address";
    }

    @RequestMapping("provinces")
    @ResponseBody
    public Result provinces() {
        try {
            List<TbProvinces> provincesAll = provincesService.getProvincesAll();
            return Result.OK().setData(provincesAll);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @RequestMapping("cities")
    @ResponseBody
    public Result cities(String provinceid) {
        try {
            List<TbCities> cityList = citiesService.getCitiesByPid(provinceid);
            return Result.OK().setData(cityList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }

    }


    @RequestMapping("areas")
    @ResponseBody
    public Result areas(String cityid) {
        try {
            List<TbAreas> areaList = areasService.getAreasByCid(cityid);
            return Result.OK().setData(areaList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @RequestMapping("getAddress")
    @ResponseBody
    public Result getAddress(@RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "5")Integer pageSize){
        try {
            PageBean all = addressService.getAll(pageNum, pageSize);
            return Result.OK().setData(all);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @RequestMapping("insertAddress")
    @ResponseBody
    public Result insertAddress(TbAddress address){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            addressService.insertAddress(address,user);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id) {
        try {
            TbAddress addressById = addressService.getAddressById(id);
            return Result.OK().setData(addressById);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.GETBYID_ERROR);
        }

    }

    @RequestMapping("updateAddress")
    @ResponseBody
    public Result updateAddress(TbAddress address) {
        try {
            addressService.updateAddress(address);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }

    }


    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Long id){
        try {
            addressService.delete(id);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }

    }


}
